package org.hcyspartnoc.luoxin.controller.system;

import jakarta.validation.Valid;
import jakarta.validation.constraints.Max;
import jakarta.validation.constraints.Min;
import org.hcyspartnoc.luoxin.common.core.AjaxResult;
import org.hcyspartnoc.luoxin.common.core.PageBean;
import org.hcyspartnoc.luoxin.common.core.annotation.Log;
import org.hcyspartnoc.luoxin.common.core.annotation.PreAuthorize;
import org.hcyspartnoc.luoxin.common.core.enums.BusinessType;
import org.hcyspartnoc.luoxin.common.exception.system.SysPermissionException;
import org.hcyspartnoc.luoxin.common.exception.system.SysRoleException;
import org.hcyspartnoc.luoxin.pojo.dto.system.SysPermissionBindingRoleDto;
import org.hcyspartnoc.luoxin.pojo.po.system.SysPermissionPo;
import org.hcyspartnoc.luoxin.pojo.vo.system.SysPermissionVo;
import org.hcyspartnoc.luoxin.service.system.ISysPermissionService;
import org.hibernate.validator.constraints.Length;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/sys/permission")
public class SysPermissionController {
    @Resource
    private ISysPermissionService sysPermissionService;

    @Log(title = "系统·权限·查询·分页", businessType = BusinessType.OTHER, isSaveResponseData = false)
    @PreAuthorize(auth = "system:permission:query:page", descr = "分页查询系统权限")
    @GetMapping("/query/page")
    public AjaxResult queryByPage(@Min(value = 1, message = "当前页不能小于1")
                                  @RequestParam(defaultValue = "1") int currentPage,
                                  @Min(value = PageBean.MIN_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                  @Max(value = PageBean.MAX_PAGE_SIZE, message = "每页大小" + PageBean.MIN_PAGE_SIZE + "-" + PageBean.MAX_PAGE_SIZE)
                                  @RequestParam(defaultValue = "5") int pageSize,
                                  @Length(max = 100, message = "认证码0-100")
                                  @RequestParam(defaultValue = "") String authCode,
                                  @Min(value = 0, message = "状态值0-2")
                                  @Max(value = 2, message = "状态值0-2")
                                  @RequestParam(defaultValue = "0") Integer status,
                                  @RequestParam(defaultValue = "0") Integer roleId) {
        PageBean<SysPermissionPo, SysPermissionVo> pageBean = sysPermissionService.queryByPage(currentPage, pageSize, authCode, status, roleId);
        return AjaxResult.success(pageBean);
    }

    @Log(title = "系统·权限·绑定·角色", businessType = BusinessType.INSERT, isSaveResponseData = false)
    @PreAuthorize(auth = "system:permission:binding:role", descr = "权限绑定角色")
    @PostMapping("/binding/role")
    public AjaxResult bindingRole(@Valid @RequestBody SysPermissionBindingRoleDto sysPermissionBindingRoleDto) throws SysPermissionException, SysRoleException {
        sysPermissionService.bindingRole(sysPermissionBindingRoleDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "系统·权限·解绑·角色", businessType = BusinessType.DELETE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:permission:unbinding:role", descr = "权限解绑角色")
    @DeleteMapping("/unbinding/role")
    public AjaxResult unbindingRole(@Valid @RequestBody SysPermissionBindingRoleDto sysPermissionBindingRoleDto) throws SysPermissionException {
        sysPermissionService.unbindingRole(sysPermissionBindingRoleDto);
        return AjaxResult.successMsg();
    }

    @Log(title = "系统·权限·修改·状态", businessType = BusinessType.UPDATE, isSaveResponseData = false)
    @PreAuthorize(auth = "system:permission:modify:status", descr = "修改权限状态")
    @PutMapping("/modify/status/{permissionId}")
    public AjaxResult modifyStatus(@PathVariable Integer permissionId) throws SysPermissionException {
        sysPermissionService.modifyStatus(permissionId);
        return AjaxResult.successMsg();
    }
}
